<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
































<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=6.6.0" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=6.6.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=6.6.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=6.6.0">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=6.6.0" color="#222">









<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    version: '6.6.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="mv命令是move的缩写，用来对文件或目录重新命名，或者将文件从一个目录移到另一个目录中，是Linux系统下常用的命令。">
<meta name="keywords" content="Linux,Shell,每天一个Linux命令">
<meta property="og:type" content="article">
<meta property="og:title" content="每天一个Linux命令（7）：mv">
<meta property="og:url" content="http://seveninsight.github.io/blog/2019/01/07/每天一个Linux命令（7）：mv/index.html">
<meta property="og:site_name" content="七年弹指间的博客">
<meta property="og:description" content="mv命令是move的缩写，用来对文件或目录重新命名，或者将文件从一个目录移到另一个目录中，是Linux系统下常用的命令。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-01-07T15:12:35.359Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="每天一个Linux命令（7）：mv">
<meta name="twitter:description" content="mv命令是move的缩写，用来对文件或目录重新命名，或者将文件从一个目录移到另一个目录中，是Linux系统下常用的命令。">






  <link rel="canonical" href="http://seveninsight.github.io/blog/2019/01/07/每天一个Linux命令（7）：mv/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>每天一个Linux命令（7）：mv | 七年弹指间的博客</title>
  












  <noscript>
  <style>
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion .logo-line-before i { left: initial; }
    .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">七年弹指间的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">知是行之始，行是知之成</p>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/blog/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/blog/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/blog/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/blog/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://seveninsight.github.io/blog/blog/2019/01/07/每天一个Linux命令（7）：mv/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="七年弹指间">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="七年弹指间的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">每天一个Linux命令（7）：mv

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-01-07 09:55:03 / 修改时间：23:12:35" itemprop="dateCreated datePublished" datetime="2019-01-07T09:55:03+08:00">2019-01-07</time>
            

            
              

              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/blog/categories/Linux/" itemprop="url" rel="index"><span itemprop="name">Linux</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/blog/categories/Linux/每天一个Linux命令/" itemprop="url" rel="index"><span itemprop="name">每天一个Linux命令</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><code>mv</code>命令是<code>move</code>的缩写，用来对文件或目录重新命名，或者将文件从一个目录移到另一个目录中，是Linux系统下常用的命令。</p>
<a id="more"></a>
<h2 id="1-功能"><a href="#1-功能" class="headerlink" title="1 功能"></a>1 功能</h2><p><code>mv</code>命令用来对文件或目录重新命名，或者将文件从一个目录移到另一个目录中。</p>
<h2 id="2-命令格式"><a href="#2-命令格式" class="headerlink" title="2 命令格式"></a>2 命令格式</h2><pre><code>mv [选项] 源文件 目标文件
</code></pre><p>【选项说明】</p>
<pre><code>--backup=&lt;备份模式&gt;：若需覆盖文件，则覆盖前先行备份；
-b：当文件存在时，覆盖前，为其创建一个备份；与 --backup 类似，但是不接受参数
-f：若目标文件或目录与现有的文件或目录重复，则直接覆盖现有的文件或目录；（默认选项）
-i：交互式操作，覆盖前先行询问用户，如果源文件与目标文件或目标目录中的文件同名，则询问用户是否覆盖目标文件。用户输入”y”，表示将覆盖目标文件；输入”n”，表示取消对源文件的移动。这样可以避免误将文件覆盖。
--strip-trailing-slashes：删除源文件中的斜杠“/”；
-S&lt;后缀&gt;：为备份文件指定后缀，而不使用默认的后缀；
--target-directory=&lt;目录&gt;：指定源文件要移动到目标目录；
-u：当源文件比目标文件新或者目标文件不存在时，才执行移动操作。
-v: 解释执行了哪些操作
</code></pre><p>【参数说明】</p>
<p>1）当第2个参数即目标文件存在时：</p>
<ul>
<li>如果目标文件是一个目录，将源文件移到目标目录中，此时参数1即源文件可以有多个；</li>
<li>如果目标文件是一个文件，将源文件重命名为目标文件名，并覆盖目标文件，此时参数1只能有一个；</li>
</ul>
<p>2）当第2个参数即目标文件不存在时：</p>
<ul>
<li>将源文件重命名为目标文件名，并覆盖目标文件，此时参数1只能有一个；</li>
</ul>
<h2 id="3-常见用法与示例"><a href="#3-常见用法与示例" class="headerlink" title="3 常见用法与示例"></a>3 常见用法与示例</h2><p><strong>1、把文件移到目标目录中</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">[work@testpc old 10:18:35]$ ll</span><br><span class="line">-rw-rw-r--. 1 work work     26 Jan  7 10:10 aaa.txt</span><br><span class="line">-rw-rw-r--. 1 work work     26 Jan  7 10:11 bbb.txt</span><br><span class="line"></span><br><span class="line">[work@testpc old 10:18:37]$ mv  aaa.txt bbb.txt dir1</span><br><span class="line"></span><br><span class="line">[work@testpc old 10:18:59]$ ll dir1</span><br><span class="line">-rw-rw-r--. 1 work work 26 Jan  7 10:10 aaa.txt</span><br><span class="line">-rw-rw-r--. 1 work work 26 Jan  7 10:11 bbb.txt</span><br></pre></td></tr></table></figure>
<p><strong>2、对文件重命名</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[work@testpc old 10:21:13]$ ll dir1</span><br><span class="line">-rw-rw-r--. 1 work work 26 Jan  7 10:10 aaa.txt</span><br><span class="line">-rw-rw-r--. 1 work work 26 Jan  7 10:11 bbb.txt</span><br><span class="line"></span><br><span class="line">[work@testpc old 10:21:16]$ mv dir1/bbb.txt dir1/ccc.py</span><br><span class="line">[work@testpc old 10:21:35]$ ll dir1</span><br><span class="line">-rw-rw-r--. 1 work work 26 Jan  7 10:10 aaa.txt</span><br><span class="line">-rw-rw-r--. 1 work work 26 Jan  7 10:11 ccc.py</span><br></pre></td></tr></table></figure>
<p><strong>3、使用<code>-b</code>选项，文件覆盖前做备份</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">[work@testpc old 10:51:25]$ ll</span><br><span class="line">drwxrwxr-x. 2 work work   4096 Jan  7 10:42 dir1</span><br><span class="line">-rwxrwxr-x. 1 work work     56 Jan  5 00:47 test.py</span><br><span class="line"></span><br><span class="line">[work@testpc old 10:51:27]$ mv -b dir1/aaa.txt test.py</span><br><span class="line">[work@testpc old 10:51:46]$ ll</span><br><span class="line">drwxrwxr-x. 2 work work   4096 Jan  7 10:51 dir1</span><br><span class="line">-rw-rw-r--. 1 work work     26 Jan  7 10:39 test.py</span><br><span class="line">-rwxrwxr-x. 1 work work     56 Jan  5 00:47 test.py~</span><br></pre></td></tr></table></figure>
<p>说明：</p>
<blockquote>
<ul>
<li><code>-b</code>不接受参数，mv会去读取环境变量VERSION_CONTROL来作为备份策略。</li>
<li><code>--backup</code>该选项指定如果目标文件存在时的动作，共有四种备份策略：<ol>
<li>CONTROL=none或off : 不备份。</li>
<li>CONTROL=numbered或t：数字编号的备份</li>
<li>CONTROL=existing或nil：如果存在以数字编号的备份，则继续编号备份m+1…n：执行mv操作前已存在以数字编号的文件log2.txt.~1~，那么再次执行将产生log2.txt~2~，以次类推。如果之前没有以数字编号的文件，则使用下面讲到的简单备份。</li>
<li>CONTROL=simple或never：使用简单备份：在被覆盖前进行了简单备份，简单备份只能有一份，再次被覆盖时，简单备份也会被覆盖。</li>
</ol>
</li>
</ul>
</blockquote>
<p><strong>4、使用<code>-i</code>选项，覆盖同名文件前询问</strong></p>
<p><strong>5、使用<code>-f</code>选项，覆盖同名文件前不询问，默认是<code>-f</code>选项。</strong></p>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/Linux/" rel="tag"># Linux</a>
          
            <a href="/blog/tags/Shell/" rel="tag"># Shell</a>
          
            <a href="/blog/tags/每天一个Linux命令/" rel="tag"># 每天一个Linux命令</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2019/01/04/每天一个Linux命令（6）：cp/" rel="next" title="每天一个Linux命令（6）：cp">
                <i class="fa fa-chevron-left"></i> 每天一个Linux命令（6）：cp
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2019/01/07/每天一个Linux命令（8）：rm/" rel="prev" title="每天一个Linux命令（8）：rm">
                每天一个Linux命令（8）：rm <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">七年弹指间</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/blog/archives/">
                
                    <span class="site-state-item-count">12</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/blog/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">7</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/blog/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">10</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-功能"><span class="nav-text">1 功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-命令格式"><span class="nav-text">2 命令格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-常见用法与示例"><span class="nav-text">3 常见用法与示例</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">七年弹指间</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v6.6.0</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script src="/blog/js/src/utils.js?v=6.6.0"></script>

  <script src="/blog/js/src/motion.js?v=6.6.0"></script>



  
  


  <script src="/blog/js/src/affix.js?v=6.6.0"></script>

  <script src="/blog/js/src/schemes/pisces.js?v=6.6.0"></script>



  
  <script src="/blog/js/src/scrollspy.js?v=6.6.0"></script>
<script src="/blog/js/src/post-details.js?v=6.6.0"></script>



  


  <script src="/blog/js/src/bootstrap.js?v=6.6.0"></script>



  



  











  





  

  

  

  

  
  

  
  

  


  
  

  

  

  

  

  

  

</body>
</html>
